<?php
/**
 * 行为与组件的demo
 */

namespace app\controllers;


use app\behaviors\EventBehavior;
use app\behaviors\My3Behavior;
use app\components\BehaviorComponent;
use app\components\EventBehaviorComponent;
use yii\base\Component;
use yii\web\Controller;

class BehaviorController extends Controller
{

    public function actionEcho()
    {
        echo "hello world!" . getcwd();
    }
    /**
     * 调用组件附加行为类的成员 属性和函数
     * curl loalhost/behavior/
     */
    public function actionIndex()
    {
        $component = new BehaviorComponent();
        $data = [
            $component->prop,
            $component->prop1,
            $component->prop2,
            $component->prop3,
        ];
        //$component->func0();
        \Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;

        //动态附加行为 对象绑定行为
        $component->attachBehaviors([My3Behavior::class]);
        $data['prop4'] = $component->prop4;
        return $data;
    }

    public function actionNew()
    {
        $component = new \yii\base\Component([ 'as myBehavior' => \app\behaviors\MyBehavior::class]);
        //var_dump($component->getBehavior('myBehavior'), $component->getBehaviors());
        $component->func0();
    }


    /**
     * 行为关联了事件
     * curl  loalhost/behavior/event
     */
    public function actionEvent()
    {
        $component = new EventBehaviorComponent();
        $component->trigger(EventBehavior::EVENT_BEFORE_VALIDATE);
    }
}